import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/app/theme/app_theme.dart';
import 'package:bxultimate/app/theme/gaps.dart';
import 'package:bxultimate/data/store/config_store.dart';
import 'package:bxultimate/resources/assets.dart';
import 'package:bxultimate/widgets/mk_card_container.dart';
import 'package:bxultimate/widgets/mk_list_cell_item.dart';
import 'package:bxultimate/widgets/mk_load_image.dart';
import 'package:bxultimate/widgets/mk_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'controller.dart';

class MerchantEnterDashboardPage extends StatefulWidget {
  const MerchantEnterDashboardPage({Key? key}) : super(key: key);

  @override
  State<MerchantEnterDashboardPage> createState() =>
      _MerchantEnterDashboardPageState();
}

class _MerchantEnterDashboardPageState
    extends State<MerchantEnterDashboardPage> {
  final controller = Get.find<MerchantEnterDashboardController>();
  final state = Get.find<MerchantEnterDashboardController>().state;

  Widget _buildBody() {
    return Padding(
        padding: EdgeInsets.only(
            left: AppTheme.iDefaultPadding, right: AppTheme.iDefaultPadding),
        child: Column(children: [
          Container(
              height: 160,
              child: Swiper(
                itemCount: state.shopDashboard.shopAnnexList!.length,
                pagination: const SwiperPagination(
                    alignment: Alignment.bottomCenter,
                    builder: TDSwiperPagination.dotsBar),
                itemBuilder: (BuildContext context, int index) {
                  return TDImage(
                    imgUrl: ConfigStore.to.baseFileUrl + state.shopDashboard.shopAnnexList![index].photo!,
                  );
                },
              )),
          Gaps.vGap8,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _buildAnalysisCard("优待人次", Assets.merchantPeople,
                  Color(0xFFFFF3F0), Color(0xFFFF8362), state.shopDashboard.discountUserCount??0),
              _buildAnalysisCard("交易金额", Assets.myOrderSfje, Color(0xFFEAEFF9),
                  Color(0xFF5B8FF9),state.shopDashboard.totalIncome??0),
            ],
          ),
          Gaps.vGap8,
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _buildAnalysisCard("优惠金额", Assets.merchantYh, Color(0xFFE9F5F1),
                  Color(0xFF5AD8A6),state.shopDashboard.discountAmount??0),
              _buildAnalysisCard("积分数", Assets.merchantScore, Color(0xFFFAF7ED),
                  Color(0xFFF6BD16),state.shopDashboard.shopPoints??0),
            ],
          ),
          Gaps.vGap8,
          _buildNav()
        ]));
  }

  _buildNav() {
    return MkCardContainer(
      padding: EdgeInsets.all(0),
      margin: EdgeInsets.all(0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          MkListCellItem(
            textIcon: MkLoadImage(Assets.merchantInformation, width: 18),
            value: '店铺信息',
            press: () {
              Get.toNamed(RouterName.userMerchantEnterInformation);
            },
          ),
          MkListCellItem(
            textIcon: MkLoadImage(
              Assets.merchantPaycode,
              width: 18,
            ),
            value: '商家收款码',
            press: () {
              Get.toNamed(RouterName.userMerchantEnterPayCode,arguments: {
                "id" :  state.shopDashboard.shop!.id!
              });
            },
          ),
          MkListCellItem(
            textIcon: MkLoadImage(
              Assets.merchantOrder,
              width: 18,
            ),
            value: '订单管理',
            press: () {},
          ),
          MkListCellItem(
            textIcon: LoadAssetImage(Assets.merchantAnnex, width: 18),
            value: '附件管理',
            press: () {
              Get.toNamed(RouterName.userMerchantEnterAnnex,arguments: {
                "id" :  state.shopDashboard.shop!.id!
              });
            },
          ),
          MkListCellItem(
            textIcon: LoadAssetImage(Assets.merchantHonourcard, width: 18),
            value: '戎耀标牌',
            press: () {
              Get.toNamed(RouterName.userMerchantEnterHonourCardIndex,arguments: {
                "id": state.shopDashboard.shop!.id!
              });
            },
          ),
          MkListCellItem(
            textIcon: LoadAssetImage(Assets.merchantProduct, width: 18),
            value: '商品管理',
            press: () {
              Get.toNamed(RouterName.userMerchantEnterProduct,arguments: {
                "id": state.shopDashboard.shop!.id!
              });
            },
          ),
        ],
      ),
    );
  }

  _buildAnalysisCard(title, icon, bgColor, textColor,num) {
    return Container(
      width: (Get.width - AppTheme.iDefaultPadding * 2.6) / 2,
      height: 80,
      padding: EdgeInsets.all(AppTheme.iDefaultPadding),
      decoration: BoxDecoration(
        color: bgColor,
        borderRadius: BorderRadius.circular(AppTheme.iDefaultPadding),
      ),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text('${title}', style: TextStyle(fontSize: 14)),
              MkLoadImage(icon, width: 20)
            ],
          ),
          Row(
            crossAxisAlignment: CrossAxisAlignment.end,
            children: [
              Text('${num}',
                  style: TextStyle(
                      fontSize: 26,
                      fontWeight: FontWeight.bold,
                      color: textColor)),
              Gaps.hGap4,
            ],
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return MkPage(
      child: Scaffold(
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          title: Obx(() => Text(
              "${state.shopDashboard.shop == null ? '商家' : state.shopDashboard.shop!.name}")),
        ),
        body: SingleChildScrollView(
          child: Obx(() => state.shopDashboard.shop == null
              ? const Center(child: CircularProgressIndicator())
              : _buildBody()),
        ),
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<MerchantEnterDashboardController>();
    super.dispose();
  }
}
